//
//  MovieCollectionViewController.m
//  豆瓣_精简版
//
//  Created by dllo on 15/11/6.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieCollectionViewController.h"
#import "MovieCollectionViewCell.h"
#import "AppTools.h"
#import "MovieDetailViewController.h"
#import "Movie.h"
@interface MovieCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,retain)NSMutableArray *movieArray;

@end

@implementation MovieCollectionViewController

- (void)dealloc
{
    [_movieArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UILabel *labe = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    labe.backgroundColor = [UIColor redColor];
    [self.view addSubview:labe];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(100, 170);
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myCollectionView];
    
    [self.myCollectionView registerClass:[MovieCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    self.movieArray = [NSMutableArray array];
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    [AppTools getDataWithURL:urlStr block:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSMutableArray *array = [dic objectForKey:@"result"];
        for (NSMutableDictionary *dic in array) {
            //把数据存入模型,再存入数组
            Movie *movie = [[Movie alloc]init];
            [movie setValuesForKeysWithDictionary:dic];
            [self.movieArray addObject:movie];
            
        }
        
        [self.myCollectionView reloadData];
    }];
    
    
}


//cell数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return self.movieArray.count;
    
}

//每个cell的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"cell";
    MovieCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.movie = [self.movieArray objectAtIndex:indexPath.row];
    return cell;
}

//点击cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MovieDetailViewController *movieDVC = [[MovieDetailViewController alloc]init];
    [self.navigationController pushViewController:movieDVC animated:YES];
    movieDVC.movie = [self.movieArray objectAtIndex:indexPath.row];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
